#if !SILVERLIGHT
using System;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Generic;
using System.Security.Principal;

namespace SBPweb.Common.Session
{
	/// <summary>
	/// Class of SessionHandler
	/// </summary>
	public class SessionHandler
	{
		#region Private members

		private static HttpSessionState mSession
		{
			get
			{
				if (HttpContext.Current == null)
				{
					return null;
				}
				return HttpContext.Current.Session;
			}
		}

		private const string KEY_ASP_NET_SessionID = "ASP.NET_SessionID";

		private static Dictionary<string, object> mAppDictionary = new Dictionary<string, object>();

		#endregion

		#region Constructor logic

		static SessionHandler()
		{
		}

		#endregion

		#region Properties

		/// <summary>
		/// Is available?
		/// </summary>
		public static bool IsAvailable
		{
			get
			{
				return mSession != null;
			}
		}

		/// <summary>
		/// Is authenticated?
		/// </summary>
		public static bool IsAuthenticated
		{
			get
			{
				return HttpContext.Current.Request.IsAuthenticated;
			}
		}

		/// <summary>
		/// ID of the session
		/// </summary>
		public static string SessionIdentifier
		{
			get
			{
				return mSession.SessionID;
			}
		}

		private const string KEY_LastException = "_LastException";
		/// <summary>
		/// Last exception
		/// </summary>
		public static System.Exception LastException
		{
			get
			{
				if (mSession == null)
				{
					return null;
				}

				return mSession[KEY_LastException] as System.Exception;
			}
			set
			{
				if (mSession == null)
				{
					return;
				}
				mSession[KEY_LastException] = value;
			}
		}

		private const string KEY_CurrentPage = "_CurrentPage";
		/// <summary>
		/// Current page
		/// </summary>
		public static string CurrentPage
		{
			get
			{
				return mSession[KEY_CurrentPage] as string;
			}
			set
			{
				if (mSession == null)
				{
					return;
				}
				mSession[KEY_CurrentPage] = value;
			}
		}

		#endregion

		#region Public methods

		/// <summary>
		/// Abandon
		/// </summary>
		public static void Abandon()
		{
//			if (IsAuthenticated)
//			{
//			}
			RemoveAppStateValues();
			mSession.Abandon();
		}

		/// <summary>
		/// Remove application state values
		/// </summary>
		public static void RemoveAppStateValues()
		{
		}

		/// <summary>
		/// GCAppStateValues
		/// </summary>
		public static void GCAppStateValues()
		{
			ArrayList gcItems = new ArrayList();

			foreach (string key in mAppDictionary.Keys)
			{
				object obj = mAppDictionary[key];

				if (obj is IPrincipal)
				{
					if (!(obj as IPrincipal).Identity.IsAuthenticated)
					{
						gcItems.Add(key);
					}
				}
			}

			foreach(string key in gcItems)
			{
				mAppDictionary.Remove(key);
			}

		}

		/// <summary>
		/// Get from the session
		/// </summary>
		/// <typeparam name="T">type</typeparam>
		/// <param name="key">key</param>
		/// <returns>item</returns>
		public static T Get<T>(string key)
		{
			if (mSession == null || mSession[key] == null)
			{
				return default(T);
			}
			return (T)mSession[key];
		}

		/// <summary>
		/// Put to the session
		/// </summary>
		/// <typeparam name="T">type</typeparam>
		/// <param name="key">key</param>
		/// <param name="value">value</param>
		public static void Put<T>(string key, T value)
		{
			if (mSession != null)
			{
				mSession.Add(key, value);
			}
		}

		/// <summary>
		/// Get from the session
		/// </summary>
		/// <typeparam name="T">type</typeparam>
		/// <param name="sessionId">id</param>
		/// <param name="key">key</param>
		/// <returns>item</returns>
		public static T Get<T>(string sessionId, string key)
		{
			if (!mAppDictionary.ContainsKey(sessionId))
			{
				return default(T);
			}
			Dictionary<string, object> dict = mAppDictionary[sessionId] as Dictionary<string, object>;

			return (T)dict[key];
		}

		/// <summary>
		/// Put in the session
		/// </summary>
		/// <typeparam name="T">type</typeparam>
		/// <param name="sessionId">id</param>
		/// <param name="key">key</param>
		/// <param name="value">value</param>
		public static void Put<T>(string sessionId, string key, T value)
		{
			Dictionary<string, object> dict;
			if (!mAppDictionary.ContainsKey(sessionId))
			{
				dict = new Dictionary<string, object>();
				mAppDictionary.Add(sessionId, dict);
			}
			else
			{
				dict = mAppDictionary[sessionId] as Dictionary<string, object>;
			}
			
			if (!dict.ContainsKey(key))
			{
				dict.Add(key, value);
			}
			else
			{
				dict[key] = value;
			}
		}

		/// <summary>
		/// Remove item
		/// </summary>
		/// <typeparam name="T">Type</typeparam>
		/// <param name="key">key</param>
		public static void Remove<T>(string key)
		{
			if (mSession != null)
			{
				mSession.Remove(key);
			}
		}

		/// <summary>
		/// Remove item
		/// </summary>
		/// <typeparam name="T">Type</typeparam>
		/// <param name="sessionId">id</param>
		/// <param name="key">key</param>
		public static void Remove<T>(string sessionId, string key)
		{
			if (mAppDictionary.ContainsKey(key))
			{
				mAppDictionary.Remove(key);
			}
		}

		#endregion

		#region Private helper methods

		private static string GenerateSessionUniqueKeyForKey(string key)
		{
			string sessionId = HttpContext.Current.Request.Params[KEY_ASP_NET_SessionID];
			if ((string.IsNullOrEmpty(sessionId)) && mSession != null)
			{
				sessionId = mSession.SessionID;
			}

			return sessionId + "_" + key;
		}

		#endregion

	}
}
#endif